import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import Home from '../components/home'
import Welcome from '../components/welcome.vue'
import Users from '../components/user/user'

Vue.use(VueRouter)

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: login },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            { path: '/welcome', component: Welcome },
            { path: '/110', component: Users }
        ]
    }
]

const router = new VueRouter({
    routes
})

//设置导航守卫
router.beforeEach((to, from, next) => {
    //to:将要访问的路由
    //from:路由从哪里来
    //next:放行。next()：直接放行 next('/login'):强制跳转至/login
    if (to.path === '/login') return next()
    const token = window.sessionStorage.getItem('token')
    if (!token) return next('/login')
    next()
})

export default router